//
//  MessageHelpGroupViewController.m
//  BGM
//
//  Created by 我要变好看 on 2018/12/21.
//  Copyright © 2018 com.alirtc.demo. All rights reserved.
//

#import "MessageHelpGroupViewController.h"
#import "Common.h"
#import "MessageCommonTableViewCell.h"
#import "BGMUser.h"
#import "BGMHTTP.h"
#import <NIMSDK/NIMSDK.h>
#import "MessageThankOtherViewController.h"
#import "SVProgressHUD.h"


@interface MessageHelpGroupViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView *myTable;
@property (nonatomic, strong)NSMutableArray *groupInfoArray;
@property (nonatomic, strong)NSMutableArray *seleArray;
@property (nonatomic, strong)UIButton *goBtn;

@end

@implementation MessageHelpGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navview.hidden = NO;
    self.titleBtn.hidden = NO;
    self.backBtn.hidden = NO;
    CGRect rect = self.titleBtn.frame;
    rect.size.width = rect.size.width+70;
    self.titleBtn.frame = rect;
    [self.titleBtn setTitle:@"帮忙完成" forState:UIControlStateNormal];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(15, self.navview.frame.size.height, SCREENW-15, 50)];
    label.text = @"您的帮忙已经完成，是否选择好友进行答谢？";
    label.textColor = bgmmainColor;
    [self.view addSubview:label];
    
    UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(15, label.frame.origin.y+label.frame.size.height, SCREENW-15, 0.3)];
    line.backgroundColor = [UIColor grayColor];
    line.alpha = 0.3;
    [self.view addSubview:line];
    
    self.seleArray = [[NSMutableArray alloc]init];
    self.groupInfoArray = [[NSMutableArray alloc]init];
    
 
    
    [self groupMessage];
    
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(seleTeamNumber) name:@"seleTeamNumber" object:nil];
    
    self.myTable = [[UITableView alloc]initWithFrame:CGRectMake(0, line.frame.origin.y+1, SCREENW, SCREENH-line.frame.origin.y-80)];
    self.myTable.delegate = self;
    self.myTable.dataSource = self;
    self.myTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.myTable];
    
    self.goBtn = [[UIButton alloc]initWithFrame:CGRectMake(30, SCREENH-70, SCREENW-60, 40)];
    self.goBtn.backgroundColor = bgmsmallColor;
    self.goBtn.layer.masksToBounds = YES;
    self.goBtn.layer.cornerRadius = 8;
    [self.goBtn setTitle:@"跳过" forState:UIControlStateNormal];
    [self.goBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.goBtn setTitle:@"确认" forState:UIControlStateSelected];
    [self.goBtn setTitleColor:bgmgrayColor forState:UIControlStateSelected];
    [self.view addSubview:self.goBtn];
    [self.goBtn addTarget:self action:@selector(clickThank:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.groupInfoArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 61;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    lable.text = @"   选择打赏";
    lable.textColor = bgmgrayColor;
    return lable;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identfier = @"MessageCommonTableViewCell";
    MessageCommonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identfier];
    if (cell == nil) {
        cell = [[MessageCommonTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identfier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.seleBtn.hidden = NO;
        cell.cellDelegate = self;
        
    }
    cell.seleBtn.tag = (int)indexPath.row;
    BOOL b = NO;
    NSString *sele = [self.seleArray objectAtIndex:indexPath.row];
    if ([sele isEqualToString:@"1"]) {
        b = YES;
    }else{
        b = NO;
    }
    [cell setGroupNumbersInfo:[self.groupInfoArray objectAtIndex:(int)indexPath.row] sele:b];
    
    return cell;
}

-(void)seleRow:(int)row isSele:(BOOL)isSele{
    if (isSele){
        [self.seleArray replaceObjectAtIndex:row withObject:@"1"];
    }else{
        [self.seleArray replaceObjectAtIndex:row withObject:@"0"];
    }
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:row inSection:0];
    [self.myTable reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"seleTeamNumber" object:nil];
}
 

- (void)groupMessage{
    for (NSString *groupid in self.userArray) {
        NIMTeam *team = [[[NIMSDK sharedSDK]teamManager] teamById:groupid];
        if([team isKindOfClass:[NIMTeam class]]){
            [self.groupInfoArray addObject:team];
            [self.seleArray addObject:@"0"];
        }else{
            [SVProgressHUD showInfoWithStatus:@"获取群组失败！"];
            break;
           
        }
        
    }
    
    [self.myTable reloadData];
}


-(void)seleTeamNumber{
    BOOL sele = NO;
    for (NSString *str in self.seleArray) {
        if ([str isEqualToString:@"1"]) {
            sele = YES;
            break;
        }
    }
    
    self.goBtn.selected = sele;
    if (sele) {  //yes
        self.goBtn.backgroundColor = bgmyelloeColor;
    }else{
        self.goBtn.backgroundColor = bgmgrayColor;
        
    }
    
}



#pragma mark ---完成帮忙  （完成之后，可能要答谢好友）
- (void)clickThank:(UIButton *)btn{
    
    if (!btn.selected) {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }else{
          [self gotoThankUserVC];
    }
 
}
- (void)gotoThankUserVC{
    if (self.goBtn.selected) {
        NSMutableArray *seleGroup = [[NSMutableArray alloc]init];
        for(int i = 0; i<self.seleArray.count; i++){
            NSString *sele = [self.seleArray objectAtIndex:i];
            if ([sele isEqualToString:@"1"]) {
                NIMTeam *team = [self.groupInfoArray objectAtIndex:i];
                [seleGroup addObject:team];
            }
        }
        
        MessageThankOtherViewController *vc =[[MessageThankOtherViewController alloc]init];
        vc.selegroupArray = seleGroup;
        [self.navigationController pushViewController:vc animated:YES];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"ThankSGroup" object:seleGroup];
        
    }else{
        [SVProgressHUD showSuccessWithStatus:@"问题已解决"];
        [self.navigationController popViewControllerAnimated:YES];
    }
}




- (void)clickBack{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
